using System;
using System.Collections.Generic;

namespace UFrame
{
    public class EventData
    {
        public int id;
        public Delegate handler;
    }


    /// <summary>
    /// 负责事件的对称释放,间接类
    /// </summary>
    public class EventControl
    {
        private List<EventData> eventDataList = new List<EventData>();

        public void RegisterEvent(int id, Action eventHandle)
        {
            RegisterEventMain(id, eventHandle);
        }

        public void RegisterEvent<T>(int id, Action<T> eventHandle)
        {
            RegisterEventMain(id, eventHandle);
        }

        public void RegisterEvent<T, T2>(int id, Action<T, T2> eventHandle)
        {
            RegisterEventMain(id, eventHandle);
        }

        public void RegisterEvent<T, T2, T3>(int id, Action<T, T2, T3> eventHandle)
        {
            RegisterEventMain(id, eventHandle);
        }

        public void RegisterEvent<T, T2, T3, T4>(int id, Action<T, T2, T3, T4> eventHandle)
        {
            RegisterEventMain(id, eventHandle);
        }

        protected void RegisterEventMain(int id, Delegate handler)
        {
            eventDataList.Add(new EventData()
            {
                id = id,
                handler = handler,
            });

            EventMa.Register(id, handler);
        }

        public void UnRegisterEvent()
        {
            if (eventDataList.Count > 0)
            {
                foreach (var item in eventDataList)
                {
                    if (item.handler != null)
                    {
                        EventMa.UnregisterMain(item.id, item.handler);
                    }
                }

                eventDataList.Clear();
            }
        }

    }
}